package com.ternnetwork.core.controller;

import java.util.List;

import javax.annotation.Resource;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ternnetwork.cms.service.content.ContentService;
import com.ternnetwork.cms.service.page.PageService;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.MvcRequestMappingUtils;
import com.ternnetwork.core.entity.Resources;
import com.ternnetwork.core.entity.Role;
import com.ternnetwork.core.enums.ResourcesType;
import com.ternnetwork.core.enums.RoleType;
import com.ternnetwork.core.service.ResourcesService;
import com.ternnetwork.core.service.RoleService;
import com.ternnetwork.core.ui.ResourcesZtree;
import com.ternnetwork.toolkit.service.campaign.CampaignPageService;






@Controller
@RequestMapping("/core/resources/*")
public class ResourcesController {
	
	@Resource
	private ResourcesService resourcesService;
	
	@Resource
	private RoleService roleService ;
	
	@Resource
	private PageService pageService;
	
	@Resource
	private ContentService  contentService;
	
	@Resource
	private CampaignPageService campaignPageService;
	
	@RequestMapping("menu/manage.htm")
	public ModelAndView menuManage(){
		List<String> mvcRequestMappingList = getUrlList();
		List<Role> list=roleService.findAll("from Role",null);
		return new ModelAndView("/WEB-INF/view/core/menu_manage.jsp").addObject("roleList",list).addObject("mvcRequestMappingList", mvcRequestMappingList);
	}
	
	
	@RequestMapping("getUrlList.htm")
	public @ResponseBody List<String> getUrlList() {
		String[] packagesToScan={"com.ternnetwork","com.fafc"};
		List<String> mvcRequestMappingList=MvcRequestMappingUtils.getMvcRequestMappingList(packagesToScan);
		mvcRequestMappingList.addAll(pageService.findAllUrl());
		mvcRequestMappingList.addAll(contentService.findAllUrl());
		mvcRequestMappingList.addAll(campaignPageService.findAllUrl());
		return mvcRequestMappingList;
	}
	
	@RequestMapping("function/manage.htm")
	public ModelAndView functionManage(){
		List<String> mvcRequestMappingList = getUrlList();
		List<Role> list=roleService.findAll("from Role",null);
		return new ModelAndView("/WEB-INF/view/core/function_manage.jsp").addObject("roleList",list).addObject("mvcRequestMappingList", mvcRequestMappingList);
	}
	
	@RequestMapping("add.htm")
	public  @ResponseBody BaseResponse add(Resources resources){
		return resourcesService.idoAdd(resources);
	}
	
	@RequestMapping("update.htm")
	public @ResponseBody BaseResponse update(Resources resources){
		return resourcesService.idoUpdate(resources);
	}
	
	@RequestMapping("delete.htm")
	public @ResponseBody BaseResponse delete(long id){
		return resourcesService.idoDeleteById(id);
	}
	
	@RequestMapping("getTreeJSON.htm")
	public @ResponseBody List<ResourcesZtree> getResourcesTreeJSON(RoleType roleType,ResourcesType resourcesType){
		return resourcesService.getTreeJSON(roleType,resourcesType);
	}
	
	@RequestMapping("getUserMenuTreeJSON.htm")
	public @ResponseBody List<ResourcesZtree> getUserMenuTreeJSON(){
		return resourcesService.getUserMenuTreeJSON();
	}

}
